<?php
class PolizasController extends AppController {

	var $name = 'Polizas';
	var $uses = array('Poliza', 'Proveedor', 'Vehiculo');
	var $helpers = array('Html', 'Form' );

	function beforeRender() {		
		$this->checkSession();
		$this->MenuCreator->setModulo($this->name);
		$this->set('menu', $this->getMenuElements());		
	}

	function index() {			
		$this->Poliza->recursive = 0;
		$this->set('polizas', $this->paginate());
	}

	function buscar(){
		if(!empty($this->data)) {
			if($this->data["Poliza"]["tipo"]!=""){
				$this->set('polizas', $this->Poliza->buscar($this->data["Poliza"]["palabra_clave"], $this->data["Poliza"]["tipo"]));
			}
			else{
				$this->Session->setFlash('Por favor elige un tipo de búsqueda');
			}
		}
	}
	
	function ver($id = null) {				
		if(!$id) {
			$this->Session->setFlash('Poliza Inválida');
			$this->redirect(array('action'=>'index'), null, true);
		}
		$this->set('Poliza', $this->Poliza->read(null, $id));
	}

	function agregar() {
		if(!empty($this->data)) {
			$this->cleanUpFields();
			$this->Poliza->create();
			
			if($this->Poliza->save($this->data)) {
				$this->Session->setFlash('La Poliza ha sido agregada.');
				$this->redirect(array('action'=>'index'), null, true);
			} else {
				$this->Session->setFlash('La Poliza no pudo ser agregada.');
			}
		}
		else{
			$this->set('proveedores', $this->Proveedor->generateList());
			$this->set('vehiculos', $this->Vehiculo->generateList());
			$this->set(compact('proveedores','vehiculos'));
		}
	}

	function editar($id = null) {
		if(!$id && empty($this->data)) {
			$this->Session->setFlash('Poliza Inválida');
			$this->redirect(array('action'=>'index'), null, true);
		}
		if(!empty($this->data)) {
			$this->cleanUpFields();
			if($this->Poliza->save($this->data)) {
				$this->Session->setFlash('La Poliza ha sido actualizada');
				$this->redirect(array('action'=>'index'), null, true);
			} else {
				$this->Session->setFlash('La Poliza no pudo ser modificada.');
			}
		}

		if(empty($this->data)) {
			$this->data = $this->Poliza->read(null, $id);
			$this->set('proveedores', $this->Proveedor->generateList());
			$this->set('vehiculos', $this->Vehiculo->generateList());
			$this->set(compact('proveedores','vehiculos'));
		}
	}

	function eliminar($id = null) {
		if(!$id) {
			$this->Session->setFlash('Poliza Inválida');
			$this->redirect(array('action'=>'index'), null, true);
		}
		if($this->Poliza->del($id)) {
			$this->Session->setFlash('La Poliza #'.$id.' ha sido eliminada');
			$this->redirect(array('action'=>'index'), null, true);
		}
	}
}
?>